<template>
	<view class="indexs">
		<view class="header header_wi" :style="'padding-top: ' + navH + 'px;'">
			<view class="header_height" :style="'height:' + gaodu + 'px;line-height:' + gaodu + 'px;top:' + gaodutops + 'px;'">
				<view class="index_h_cont3">
					<view class="index_wenben_img" @tap="jumpzuo"><image :src="imgUrl + '/dzx_img33.png'" mode="widthFix"></image></view>
					<view class="index_wenben">{{ $t('page_pintuans_dizhi.Address_management') }}</view>
				</view>
			</view>
			<view class="coloe_title">s</view>
		</view>
		<view class="index_center" :style="'padding-top:' + (navH + 50) + 'px;padding-bottom:130rpx;'">
			<view class="dizhi_center">
				<view class="dizhi_item1">
					<view class="dizhi_item1_list" v-if="arrlist.length != 0">
						<view class="dizhi_item1_cont" @tap="xuanze" :data-id="item.address_id" v-for="(item, index) in arrlist" :key="index">
							<view class="dizhi_item1_cont_t">
								<view class="dizhi_item1_cont_l">
									<view class="dizhi_item1_cont_t1">{{ item.name }} {{ item.phone }}</view>
									<view class="dizhi_item1_cont_t2">
										{{ item.country }}{{ item.province }}{{ item.city }}{{ item.district }}{{ item.detail }} {{ item.door }}
									</view>
								</view>
								<view class="dizhi_item1_cont_r">
									<view class="dizhi_item1_cont_r_btns" @tap.stop.prevent="bianji" :data-id="item.address_id">
										<image :src="imgUrl + '/dzx_img81.png'"></image>
									</view>
								</view>
							</view>

							<view class="dizhi_item1_cont_b">
								<view v-if="address_id != item.address_id" class="dizhi_item1_cont_moren" @tap.stop.prevent="setmoren" :data-id="item.address_id">{{ $t('page_pintuans_dizhi.SetMoren') }}</view>
								<view v-if="address_id == item.address_id" class="dizhi_item1_cont_moren" >{{ $t('page_pintuans_dizhi.Moren') }}</view>
								<view class="dizhi_item1_cont_shan" @tap.stop.prevent="shanchu" :data-id="item.address_id">{{ $t('page_pintuans_dizhi.delete') }}</view>
							</view>
						</view>
					</view>
					<view class="dizhi_item1_kong" v-else>
						<image :src="imgUrl + '/dzx_img80.png'"></image>
						<text>{{ $t('page_pintuans_dizhi.No_delivery_address') }}</text>
					</view>
				</view>
				<view class="dizhi_item2">
					<view class="baoguo_btns">
						<view class="baoguo_btns_posi">
							<view class="baoguo_btn" @tap="goadddizhi">{{ $t('page_pintuans_dizhi.New_address') }}</view>
						</view>
					</view>
				</view>
			</view>
		</view>
	</view>
</template>

<script>
// pages/shouye/shouye_zkc/shouye_zkc.js
const app = getApp();

let request = require('../../../utils/require');

export default {
	data() {
		return {
			navH: app.globalData.navHeight,
			gaodu: app.globalData.height,
			gaodutops: app.globalData.top,
			imgUrl: app.globalData.imgUrl,
			ischoose: 1,
			arrlist: [],
			type: '',
			address_id: ''
		};
	},
	/**
	 * 生命周期函数--监听页面加载
	 */ onLoad(options) {
		var that = this;
		that.type= options.type
	},
	onShow() {
		var that = this;
		that.dizhilist();
	},
	/**
	 * 生命周期函数--监听页面初次渲染完成
	 */
	onReady: function() {},
	/**
	 * 生命周期函数--监听页面隐藏
	 */
	onHide: function() {},
	/**
	 * 生命周期函数--监听页面卸载
	 */
	onUnload: function() {},
	/**
	 * 页面相关事件处理函数--监听用户下拉动作
	 */
	onPullDownRefresh: function() {},
	/**
	 * 页面上拉触底事件的处理函数
	 */
	onReachBottom: function() {},
	/**
	 * 用户点击右上角分享
	 */
	onShareAppMessage: function() {},
	methods: {
		shanchu(e) {
			var that = this;
			uni.showModal({
				title: '确认删除？',
				success(res) {
					if (res.confirm) {
						var data = {
							token: uni.getStorageSync('token'),
							address_id: e.currentTarget.dataset.id
						};
						request.post(`index.php?s=/api/address/delete&wxapp_id=` + uni.getStorageSync('web_id'), data).then(res => {
							if (res.data.code == 1) {
								uni.showToast({
									title: res.data.data
								});
								setTimeout(() => {
									that.arrlist = [];
									that.dizhilist();
								}, 1000);
							} else {
								uni.showToast({
									title: res.data.msg,
									icon: 'none'
								});
							}
						});
					} else {
						if (res.cancel) {
							console.log('用户点击取消');
						}
					}
				}
			});
		},
		setmoren(e){
			var that = this;
			uni.showModal({
				title: '确认设置为默认路线？',
				success(res) {
					if (res.confirm) {
						var data = {
							token: uni.getStorageSync('token'),
							address_id: e.currentTarget.dataset.id
						};
						request.post(`index.php?s=/api/address/setDefault&wxapp_id=` + uni.getStorageSync('web_id'), data).then(res => {
							if (res.data.code == 1) {
								uni.showToast({
									title: res.data.data
								});
								setTimeout(() => {
									that.arrlist = [];
									that.dizhilist();
								}, 1000);
							} else {
								uni.showToast({
									title: res.data.msg,
									icon: 'none'
								});
							}
						});
					} else {
						if (res.cancel) {
							console.log('用户点击取消');
						}
					}
				}
			});
		},
		dizhilist() {
			var that = this;
			var data = {
				token: uni.getStorageSync('token')
			};
			request.post(`index.php?s=/api/address/lists&wxapp_id=` + uni.getStorageSync('web_id'), data).then(res => {
				if (res.data.code == 1) {
					that.arrlist= res.data.data.list
					that.address_id=res.data.data.default_id
					console.log(res, ';ressssssss');
				}
			});
		},

		bianji(e) {
			uni.navigateTo({
				url: '/pages/pintuans/dizhi_add/dizhi_add?type=1&id=' + e.currentTarget.dataset.id
			});
		},

		goadddizhi() {
			uni.navigateTo({
				url: '/pages/pintuans/dizhi_add/dizhi_add?type=0'
			});
		},

		jumpzuo() {
			uni.navigateBack();
		},

		xuanze(e) {
			// #ifdef MP-WEIXIN
			if (this.type == 'xuanze') {
				var pages = getCurrentPages();
				var prevPage = pages[pages.length - 2];
				console.log(prevPage,89765)
				console.log(prevPage.data.address_id,9999);
				prevPage.data.address_id = e.currentTarget.dataset.id;
				prevPage.data.ischoose = 1;
				uni.navigateBack({
					delta: 1
				});
			}
			// #endif
			// #ifdef H5
			if (this.type == 'xuanze') {
				var pages = getCurrentPages();
				var prevPage = pages[pages.length - 2];
				console.log(prevPage,89765)
				console.log(prevPage.address_id,9999);
				prevPage.address_id = e.currentTarget.dataset.id;
				prevPage.ischoose = 1;
				uni.navigateBack({
					delta: 1
				});
			}
			// #endif
		}
	}
};
</script>
<style>
@import './dizhi.css';
</style>
